package com.ivt.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ivt.core.ex.IvtException;
import com.ivt.dao.NoticeDao;
import com.ivt.dao.SpecificDao;
import com.ivt.model.Notice;
import com.ivt.model.view.QueryCriteria;
import com.ivt.service.NoticeService;
import com.ivt.utils.enums.AuditStatus;

@Service
public class NoticeServiceImpl implements NoticeService {

	@Resource
	private NoticeDao noticeDao;

	@Resource
	private SpecificDao specificDao;

	@Override
	public Notice create(Notice notice) throws IvtException {
		if (null == notice) {
			throw new IvtException("公告信息不能为空");
		}
		if (StringUtils.isBlank(notice.getNoticeTitle())) {
			throw new IvtException("公告名称不能为空");
		}
		if (StringUtils.isBlank(notice.getContent())) {
			throw new IvtException("公告内容不能为空");
		}

		notice.setNoticeId(null);
		notice.setStatus(AuditStatus.PASS);
		return noticeDao.saveAndFlush(notice);
	}

	@Override
	public Notice update(Notice notice) throws IvtException {
		if (null == notice || null == notice.getNoticeId()
				|| 0 >= notice.getNoticeId()) {
			throw new IvtException("公告信息不能为空");
		}

		notice.setStatus(AuditStatus.PASS);
		return noticeDao.saveAndFlush(notice);

	}

	@Override
	public long queryCount(QueryCriteria queryCriteria) throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryNoticeCount(queryCriteria);
	}

	@Override
	public List<Notice> queryClassesByCriteria(QueryCriteria queryCriteria)
			throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryNotice(queryCriteria);
	}

	@Override
	public Notice delete(Long noticeId) throws IvtException {
		if (null == noticeId || 0 >= noticeId) {
			throw new IvtException("参数错误");
		}

		Notice notice = new Notice();
		notice.setNoticeId(noticeId);
		notice.setStatus(AuditStatus.NOT);
		return noticeDao.saveAndFlush(notice);
	}

	@Override
	public int deletes(Long[] noticeIds) throws IvtException {
		if (ArrayUtils.isEmpty(noticeIds)) {
			throw new IvtException("参数错误");
		}

		Set<Long> cleanIds = Sets.newHashSet(noticeIds);
		Iterable<Notice> notice = null;
		notice = noticeDao.findAll(cleanIds);
		int i = 0;
		if (null != notice) {
			for (Notice cz : notice) {
				cz.setStatus(AuditStatus.NOT);
				noticeDao.saveAndFlush(cz);
				i++;
			}
		}
		return i;
	}

	@Override
	public Notice getNotice(Long noticeId) throws IvtException {
		if (null == noticeId || 0 >= noticeId) {
			throw new IvtException("参数错误");
		}
		return noticeDao.findOne(noticeId);
	}

	@Override
	public List<Notice> getNotice(Set<Long> noticeId) throws IvtException {
		if (CollectionUtils.isEmpty(noticeId)) {
			throw new IvtException("参数错误");
		}

		Iterable<Notice> coll = noticeDao.findAll(noticeId);
		return (null == coll) ? null : Lists.newArrayList(coll);
	}

	protected QueryCriteria cleanQueryCriteria(QueryCriteria queryCriteria) {

		if (null == queryCriteria) {
			return null;
		}
		Map<String, Object> criteria = queryCriteria.splitCriteria();

		if (MapUtils.isNotEmpty(criteria)) {

			if (criteria.containsKey("status")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("status")))) {
				AuditStatus status = AuditStatus.findByValue(Integer
						.valueOf(String.valueOf(criteria.get("status"))));
				criteria.put("status", status);
			}
		}

		return queryCriteria;
	}
}
